package com.znsb.msfq.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.BaseFragment;
import com.znsb.msfq.R;
import com.znsb.msfq.activity.ShowBillDetailActvity;
import com.znsb.msfq.adapter.MyShowBillRVAdapter;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.H3DiscussitemBean;
import com.znsb.msfq.bean.ShowBillBean;
import com.znsb.msfq.utils.GsonUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.NetworkAddress;
import com.znsb.msfq.utils.OnItemClickListener;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.view.EndLessOnScrollListener;
import com.znsb.msfq.view.MyLinearLayoutManager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 名称：晒单
 * 创建时间：2016/10/20.
 * 作者：杨广
 * 手机号：17382373271
 * 说明：
 */

public class Home3Fragment extends BaseFragment implements OnItemClickListener {

    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.list_recyler_view)
    RecyclerView listRecylerView;
    @Bind(R.id.list_swiperefresh_layout)
    SwipeRefreshLayout listSwiperefreshLayout;


    private MyShowBillRVAdapter adapter;
    private MyLinearLayoutManager layoutManager;
    private int pageNo = 1;

    private boolean mIsRefreshing = false;

    @Override
    protected int getlayoutView() {
        return R.layout.list_recyler_view;
    }

    @Override
    protected void initView(View view) {
        barImgBack.setVisibility(View.GONE);
        barTvTitle.setText("晒单");
        /**
         * 列表属性配置
         */
        layoutManager = new MyLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        listSwiperefreshLayout.setColorSchemeResources(R.color.blueStatus);
        listSwiperefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                listSwiperefreshLayout.setRefreshing(true);
            }
        });
        listRecylerView.setLayoutManager(layoutManager);
        listSwiperefreshLayout.setOnRefreshListener(onRefresh);
        /**
         * 下拉加载更多
         */
        listRecylerView.addOnScrollListener(new EndLessOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                pageNo++;
                getComments(pageNo);
            }
        });
        adapter = new MyShowBillRVAdapter(getActivity(), list);
        listRecylerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        listRecylerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!listSwiperefreshLayout.isRefreshing()) {
                    mIsRefreshing = false;
                }
                if (mIsRefreshing) {
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    @Override
    protected void initData() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pageNo = 1;
                list.clear();
                getComments(pageNo);
                listSwiperefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }

    @Override
    protected void clickListener(View view) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate recyler_divider fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    private List<ShowBillBean.DataListBean> list = new ArrayList<>();
    private List<ShowBillBean.DataListBean> dataList = new ArrayList<>();

    //商品分类列表
    public void getComments(int pageNo) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageNo", pageNo);
        maps.put("pageSize", 10);
        HttpUtils.getInstance(getActivity()).postAsnyRequest(NetworkAddress.Comments, HttpUtils.getMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String code, JSONObject request) {
                LogUtils.i("TAG:", "getComments=" + request.toString());
                ShowBillBean data = (ShowBillBean) GsonUtils.getGsonData(ShowBillBean.class, request.toString());
                if (data != null) {
                    dataList = data.getDataList();
                    Message message = new Message();
                    message.obj = dataList;
                    message.what = 100;
                    handler.sendMessageDelayed(message, 100);
                }
            }

            @Override
            public void onFailure(String result) {
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 100:
                    dataList = (List<ShowBillBean.DataListBean>) msg.obj;
                    if (dataList != null && dataList.size() > 0) {
                        list.addAll(dataList);
                    }
                    adapter.updateList(list);
                    break;
            }

        }
    };
    /**
     * 下拉刷新
     */
    private SwipeRefreshLayout.OnRefreshListener onRefresh = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mIsRefreshing = true;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    pageNo = 1;
                    list.clear();
                    getComments(pageNo);
                    listSwiperefreshLayout.setRefreshing(false);
                }
            }, 1000);
        }
    };

    /**
     * 列表item点击事件
     */
    @Override
    public void onItemClick(View view, int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", list.get(position).getId() + "");
        map.put("type", "1");
        IntentUtils.startActivityAnimGeneral(getActivity(), ShowBillDetailActvity.class, map);
    }
}
